package filter;

/**
 * @Author Su
 * @Date 2025/4/30 21:57
 * @Version 1.0 （版本号）
 */
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class CORSFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }



    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 允许的前端域名
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
        // 允许的请求方法
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        // 允许的请求头
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        // 允许携带认证信息（cookies等）
        response.setHeader("Access-Control-Allow-Credentials", "true");
        // 预检请求的有效期，单位：秒
       // response.setHeader("Access-Control-Max-Age", "3600");

            filterChain.doFilter(servletRequest, response);
    }

    @Override
    public void destroy() {
    }
}